﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using Common;
using DataAccessLayer;
namespace VMS_Web
{
    public partial class RateMaster : System.Web.UI.Page
    {

        protected MainMaster CurrentMasterPage = null;

        List<Master_Items> ObjLstMaster_Items = null;
        DataTable ObjDataTable = null;

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            CurrentMasterPage = (MainMaster)this.Master;
            
        }

        protected void Page_Init(object sender, EventArgs e)
        {
          //  grdunitcode.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
          
           
            if (!Page.IsPostBack)
            {
                ddlMasters.DataSource = GenericUtility.GetDropDownData(GenericUtility.MASTERTYPES);
                ddlMasters.DataBind();

                if (Cache["Master_Items"] == null)
                {
                    ObjLstMaster_Items = new Master_Items_DB().GetAllMastersForRateMaster();
                }
                else
                {
                    ObjLstMaster_Items = new Master_Items_DB().GetAllMastersForRateMaster();
                    Cache["Master_Items"] = ObjLstMaster_Items;
                }
               // BindRateStructure();
                BindChargeCodes();
                BindUnitCodes();
                BindGrid();

            }
          }
     
        protected void ddlMasters_SelectedIndexChanged(object sender, EventArgs e)
        {
            string value = ddlMasters.SelectedValue;

            if (Cache["Master_Items"] != null)
            {
                ObjLstMaster_Items = Cache["Master_Items"] as List<Master_Items>;

                ddlmasteritems.DataSource = from item in ObjLstMaster_Items
                                where item.Master_Item == ddlMasters.SelectedValue
                                select item;
                ddlmasteritems.DataTextField = "Item_Value";
                ddlmasteritems.DataValueField = "Item_Value";
                ddlmasteritems.DataBind();
                
            }
            else
            {
                ObjLstMaster_Items = new Master_Items_DB().GetAllMastersForRateMaster();
                Cache["Master_Items"] = ObjLstMaster_Items;
                ddlmasteritems.DataSource = from item in ObjLstMaster_Items
                                            where item.Master_Item == ddlMasters.SelectedValue
                                            select item;
                ddlmasteritems.DataTextField = "Item_Value";
                ddlmasteritems.DataValueField = "Item_Value";
                ddlmasteritems.DataBind();
               
            }

         
        }

        protected void btnadditem_ServerClick(object sender, EventArgs e)
        {
            lstbxitems.Items.Add(ddlmasteritems.SelectedItem.Value);
        }

        protected void btnremoveitem_ServerClick(object sender, EventArgs e)
        {
            try
            {
                if (lstbxitems.SelectedIndex != null)
                {
                    lstbxitems.Items.RemoveAt(lstbxitems.SelectedIndex);
                }
            }
            catch (Exception exp) { }
        }

        protected void btnaddcustunitcode_ServerClick(object sender, EventArgs e)
        {
           // grdunitcode.DataSource = ViewState["grdunitcode"];
           // DataTable datatable = DataFromDataGrodToDataTable(grdunitcode, ObjDataTable);
            ObjDataTable = ViewState["ObjDataTable"] as DataTable;
            ObjDataTable = AddColumnToDataTable(ddlunitcodes.SelectedValue.ToString(), ObjDataTable);
            ViewState["ObjDataTable"] = ObjDataTable;
            grdunitcode.DataSource = ObjDataTable;
            grdunitcode.DataBind();
           
            
        }

        protected void btnnew_ServerClick(object sender, EventArgs e)
        {




        }

        public void BindChargeCodes()
        {
            ddlchargecodes.DataSource = GenericUtility.GetDropDownData(GenericUtility.CHARGECODES);
            ddlchargecodes.DataBind();

        }

        public void BindUnitCodes()
        {
            ddlunitcodes.DataSource = GenericUtility.GetDropDownData(GenericUtility.DEFAULTUNITCODES);
            ddlunitcodes.DataBind();

        }

        private TextBox CreateTextBox(string name)
        {
           
            TextBox textbox = new TextBox();
            textbox.Width = Unit.Point(50);
            textbox.EnableViewState = true;
            textbox.ID = name;
            textbox.Attributes["Runat"] = "server";
            textbox.TextChanged += new EventHandler(textbox_TextChanged);
           
            return textbox;
        }

        void textbox_TextChanged(object sender, EventArgs e)
        {
            string st = "hi";
        }

        private void BindGrid()
        {
            ObjDataTable = new RateManagement_DB().GenerateRateMasterStructure();
            ViewState["ObjDataTable"] = ObjDataTable;
            grdunitcode.DataSource = ObjDataTable;
            grdunitcode.DataBind();

        }

        private void TextBoxesForCells()
        {
            if (grdunitcode.Items.Count > 0)
            {
                DataGridItem firstrow = grdunitcode.Items[0];

                ViewState["ObjDataTable"] = ObjDataTable;
                ViewState["grdunitcode"] = grdunitcode.DataSource;
               // grdunitcode.EnableViewState = true;

            }

        }

        private DataGrid AddControlToCells(DataGrid datagrid, int cell_no)
        {
            DataGridItem firstrow = grdunitcode.Items[0];

            foreach (DataGridItem row in grdunitcode.Items)
            {
                string name = row.Cells[cell_no].Text + firstrow.Cells[cell_no].Text;
                row.Cells[cell_no].Controls.Add(CreateTextBox(name));

            }


            return datagrid;

        }

        private DataTable AddColumnToDataTable(string column_name, DataTable datatable)
        {
            try
            {
                DataColumn column = new DataColumn(column_name);
                datatable.Columns.Add(column);

                
            }
            catch (DuplicateNameException dnexp)
            {
                CurrentMasterPage.MessageToDisplay(MessageType.Warning,"Cannot add this column as it is already existed");

            }
            return datatable;
        }

        private DataGrid AddColumnToDridView(string column_name, DataGrid gridview)
        {
            BoundColumn column= new BoundColumn();
            column.HeaderText=column_name;
            gridview.Columns.Add(column);
            return gridview;

        }

        private DataTable DataFromDataGrodToDataTable(DataGrid datagrid,DataTable datatable)
        {
            datatable = datagrid.DataSource as DataTable;

            return datatable;

        }

        protected void grdunitcode_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
           
              DataGridItem row = e.Item;

              if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
               {
                for (int column = 1; column < row.Cells.Count; column++)
                {
                    string name = row.Cells[0].Text + row.ItemIndex.ToString();
                    row.Cells[column].Controls.Add(CreateTextBox(name));
                }
            
        }

            
            //if (e.Item.ItemIndex >0)
            //{
           
            //        string name;
            //        for (int column = 1; column < e.Item.Cells.Count; column++)
            //        {
            //            name = grdunitcode.Items[e.Item.ItemIndex-1].Cells[0].Text + e.Item.ItemIndex.ToString();
            //            grdunitcode.Items[e.Item.ItemIndex-1].Cells[column].Controls.Add(CreateTextBox(name));
                        
            //        }
            //}
        }

             
   
    }
}